package com.zsp.coupon.service.imp;

import com.zsp.coupon.dao.CouponTemplateDao;
import com.zsp.coupon.entity.CouponTemplate;
import com.zsp.coupon.exception.CouponException;
import com.zsp.coupon.service.ITemplateBaseService;
import com.zsp.coupon.vo.CouponTemplateSDK;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <h1>优惠券模板基础服务接口实现</h1>
 * Created by zsp.
 */
@Service
public class ITemplateBaseServiceImpl implements ITemplateBaseService {


    private  final CouponTemplateDao templateDao;

    @Autowired
    public ITemplateBaseServiceImpl(CouponTemplateDao templateDao) {
        this.templateDao = templateDao;
    }


    /**
     * <h2>根据优惠券模板 id 获取优惠券模板信息</h2>
     * @param id 模板 id
     * @return {@link CouponTemplate} 优惠券模板实体
     */
    @Override
    public CouponTemplate buildTemplateInfo(Integer id) throws CouponException {
        Optional<CouponTemplate> template =templateDao.findById(id);
        if (! template.isPresent()){
            throw  new CouponException("Template  Is not Exception"+id);
        }

        return template.get();
    }

    /**
     * <h2>查找所有可用的优惠券模板</h2>
     *
     * @return {@link CouponTemplateSDK}s
     */
    @Override
    public List<CouponTemplateSDK> findAllUsableTemplate() {
        List<CouponTemplate> templates=
                templateDao.findAllByAndAvailabeAndExpired(true,false);
        return templates.stream().map(this::template2SDKtemplateSdk).collect(Collectors.toList());

    }

    /**
     * <h2>获取模板 ids 到 CouponTemplateSDK 的映射</h2>
     * @param ids 模板 ids
     * @return Map<key: 模板 id, value: CouponTemplateSDK>
     */
    @Override
    public Map<Integer, CouponTemplateSDK> findIds2TemplateSDK(Collection<Integer> ids) {
        List<CouponTemplate> templates = templateDao.findById(ids);

        return templates.stream().map(this::template2SDKtemplateSdk)
                .collect(Collectors.toMap(
                        CouponTemplateSDK::getId, Function.identity()
                ));

    }

    /**
     * <h2>将 CouponTemplate 转换为 CouponTemplateSDK</h2>
     * */

    private CouponTemplateSDK template2SDKtemplateSdk(CouponTemplate template){
        return  new CouponTemplateSDK(
                template.getId(),
                template.getName(),
                template.getLogo(),
                template.getDesc(),
                template.getCategory().getCode(),
                template.getProdcutLine().getCode(),
                template.getKey(),
                template.getTarget().getCode(),
                template.getRule()
        );
    }
}
